﻿@model TbShop.PageInfo
<div class="mini-page">
    <style>
        .index {
            cursor: pointer;
        }
    </style>
    <div class="mini-page-content">
        @{
            var paras = string.Empty;
            var fix = "";
            if (string.IsNullOrEmpty(Model.CustomParam))//CustomParam为空或null时，表明控制器没有指定查询条件，仍通过QueryString获取查询条件
            {
                foreach (var q in Request.QueryString)
                {
                    var key = q.ToString();
                    if (key == Model.Prefix + "_pi")
                    {
                        continue;
                    }
                    paras += "&" + key + "=" + Request.QueryString[key];
                }
                fix = this.Request.QueryString.HasKeys() ? paras : "";
            }
            else
            {
                fix = Model.CustomParam;
            }

            var arrayNumber = "";
            var halfArrayNumber = 4;
            int i, j;
            arrayNumber += "<a style='display:" + (Model.PageIndex > 1 ? "" : "none") + "' class='" + Model.Prefix + "index index start_page mobile_hide' idx='1' get_href='?" + Model.QueryKey + "=1" + fix + "' class='disabled' title='首页'>首页</a>";
            for (i = Model.PageIndex - halfArrayNumber, i = (i >= 1 ? i : i = 1), j = Model.PageIndex + halfArrayNumber, j = (j < Model.PageCount ? j : j = Model.PageCount); i <= j; i++)
            {
                arrayNumber += "<a class='" + Model.Prefix + "index index normal_page " + (i == Model.PageIndex ? "current" : "") + "' idx='" + i + "' get_href='?" + Model.QueryKey + "=" + i + "" + fix + "'>" + i + "</a>";
            }
            arrayNumber += "<a style='display:" + (Model.PageIndex < Model.PageCount ? "" : "none") + "' class='" + Model.Prefix + "index index end_page mobile_hide' idx='" + (Model.PageCount) + "' get_href='?" + Model.QueryKey + "=" + (Model.PageCount) + "" + fix + "' title='末页'>末页</a>";
            arrayNumber = "<span id='" + Model.Prefix + "navv'>" + arrayNumber + "</span>";
        }
        @Html.Raw(arrayNumber)
        @{
            if (Model.HttpMethod.ToUpper() == "POST")
            {
                <form method="post" id="@(Model.Prefix + "_form")" action="@Request.Url.LocalPath">
                    @{
                if (Request.Form[Model.QueryKey] == null)
                {
                    <input type="hidden" id="@(Model.QueryKey)" name="@(Model.QueryKey)" value="" />
                }
                    }
                    @{var keys = Request.Form.AllKeys;
                    foreach (var key in keys)
                    {
                        <input type="hidden" id="@(Model.Prefix + key)" name="@key" value="@Request.Form[key]" />
                    }
                    }
                </form>
                <script>
                    $(document).ready(function () {
                        $('.@Model.Prefix' + "index").click(function () {
                            if ($(this).attr('class').indexOf('current') == -1) {
                                $('#@Model.QueryKey').val($(this).attr('idx'));
                                $('#@(Model.Prefix + "_form")').submit();
                            }
                        });
                    });
                </script>
            }
            else if (Model.HttpMethod.ToUpper() == "AJAX")
            {
                var keys = Request.Form.AllKeys;
                var pars = "";
                for (int k = 0; k < keys.Count(); k++)
                {
                    pars += Request.Form.AllKeys[k] + "|" + Request.Form[k] + "?";
                }
                var getKeys = Request.QueryString.AllKeys;
                for (int k = 0; k < getKeys.Count(); k++)
                {
                    pars += Request.QueryString.AllKeys[k] + "|" + Request.QueryString[k] + "?";
                }
                <script>
                    $(document).ready(function () {
                        if ("@(Model.Prefix+"pagination")") {
                            register_click_@(Model.Prefix)();
                        } else {
                            //throw new DOMException("未找到页面JS分页方法");
                            console.log('未找到页面JS分页方法:(Prefix:@Model.Prefix)');
                        }
                    });

                    function register_click_@(Model.Prefix)() {
                        $('.@(Model.Prefix+"index")').unbind('click').click(function () {
                            if ($(this).attr('class').indexOf('current') == -1) {
                                var pageIndex = $(this).attr('idx');
                                var parStr = "@pars";
                                var parameters = {};
                                if (parStr.length > 0) {
                                    var pars = parStr.split('?');
                                    for (var j = 0; j < pars.length; j++) {
                                        if (pars[j].length > 0) {
                                            var kv = pars[j].split('|');
                                            parameters[kv[0]] = kv[1];
                                        }
                                    }
                                }
                                window["@(Model.Prefix+"pagination")"](pageIndex, parameters);
                                load_page_@(Model.Prefix)("@Model.Prefix", parseInt(pageIndex))
                                if (pageIndex > 1) {
                                    $('#@(Model.Prefix + "navv") .start_page').show();
                                } else {
                                    $('#@(Model.Prefix + "navv") .start_page').hide();
                                }
                                if (pageIndex < parseInt("@Model.PageCount")) {
                                    $('#@(Model.Prefix + "navv") .end_page').show();
                                } else {
                                    $('#@(Model.Prefix + "navv") .end_page').hide();
                                }
                            }
                        });
                    }

                    function load_page_@(Model.Prefix)(prefix, PageIndex) {
                        var arrayNumber = "";
                        var halfArrayNumber = 5;
                        var i = 0, j = 0;
                        var pageHtml = "";
                        for (i = PageIndex - halfArrayNumber, i = (i >= 1 ? i : i = 1), j = PageIndex + halfArrayNumber, j = (j < parseInt("@Model.PageCount") ? j : j = parseInt("@Model.PageCount")) ; i <= j; i++) {
                            if (i == PageIndex) {
                                pageHtml += "<a class='" + "@Model.Prefix" + "index index normal_page current' idx='" + i + "' get_href='?" + "@Model.QueryKey" + "=" + i + "" + "@fix" + "'>" + i + "</a>";
                            } else {
                                pageHtml += "<a class='" + "@Model.Prefix" + "index index normal_page' idx='" + i + "' get_href='?" + "@Model.QueryKey" + "=" + i + "" + "@fix" + "'>" + i + "</a>";
                            }
                        }
                        $('#' + prefix + 'navv .normal_page').remove();
                        $('#' + prefix + 'navv .start_page').after(pageHtml);
                        register_click_@(Model.Prefix)();
                    }
                </script>
            }
            else
            {
                <script>
                    $(document).ready(function () {
                        $('.index').click(function () {
                            if ($(this).attr('class').indexOf('current') == -1) {
                                location.href = $(this).attr("get_href")
                            }
                        });
                    });
                </script>
            }
        }
    </div>
</div>